<template>
	<view class="page">
		<!-- 页面头部内容开始 -->
		<vk-data-page-header
			:title="page.title"
			:show-back="page.showBack"
			@back="pageBack"
		></vk-data-page-header>
		<!-- 页面头部内容结束 -->
		<view class="page-body">
			<!-- 页面主体内容开始 -->
			<view style="margin-bottom: 10px;">
				<view class="tips">1、Lv.0 为用户一注册就拥有的等级，为非会员</view>
				<view class="tips">2、各条件之间满足任意一项，即可升级。</view>
			</view>
			<vk-data-form
				ref="form1"
				v-loading="loading"
				v-model="form1.data"
				:rules="form1.props.rules"
				:action="form1.props.action"
				:form-type="form1.props.formType"
				:columns='form1.props.columns'
				:loading.sync="form1.props.loading"
				:labelWidth="form1.props.labelWidth"
				:footer-show="false"
				@success="formSuccess"
			></vk-data-form>
			<!-- 页面主体内容结束 -->
		</view>
		<view class="page-footer">
			<!-- 页面底部内容开始 -->
			<el-button plain style="margin-right: 20px;width: 80px;" @click="resetForm" :loading="form1.props.loading">重置</el-button>
			<el-button type="primary" style="width: 80px;" @click="submitForm" :loading="form1.props.loading">{{ page.submitText }}</el-button>
			<!-- 页面底部内容开始 -->
		</view>
		<view class="page-dialog">
			<!-- 页面弹窗内容开始 -->

			<!-- 页面弹窗内容开始 -->
		</view>
	</view>
</template>

<script>
	var that;													// 当前页面对象
	var vk = uni.vk;									// vk实例
	export default {
		data() {
			// 页面数据变量
			return {
				// init请求返回的数据
				data:{

				},
				loading: false,
				page: {
					title: "会员等级",
					submitText: "设置",
					cancelText: "返回",
					showBack: false,
					backPage:"",
				},
				form1: {
					// 表单请求数据，此处可以设置默认值
					data: {

					},
					// 表单属性
					props: {
						// 表单请求地址
						action: "admin/business/sys/mch.updateVipLevelRule",
						// 表单字段显示规则
						columns: [
							// 常用字段类型
							{
								key:"vip_level_rule", title:"", type:"array<object>", showLabel:false,
								size:"small",
								showAdd:true,
								showClear:false,
								showSort:false,
								// 新增一行时,该行的默认值
								defaultValue:{
									name:"",
									discount:"",
									total_recharge_amount:"",
									total_order_amount:"",
									is_free_shipping:false,
									free_shipping_min_order_amount:"",
									integral_ratio:1
								},
								maxlength:10,
								rightBtns:[
									{
										mode:'delete',
										title:'删除',
										show: (item,index)=>{
											return index > 0;
										}
									}
								],
								columnIndexMethod:function(index){
									let str = `Lv.${index}`;
									return str;
								},
								// 每行每个字段对应的渲染规则
								columns:[
									{
										key:"name", title:"等级名称", type:"text", width:160, clearable:false, placeholder:"输入等级名称", disabled:"lv==0",
										isUnique:true,// 不可重复
										rules:[
											{ required:true, message:"该项不能为空", trigger:["change","blur"] },
											{ min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: ["change","blur"] }
										]
									},
									{
										key:"discount", title:"会员折扣", type:"discount", placeholder:"输入折扣", width:160, incMode: -2, clearable:false,
										rules:[
											{ required:true, message:"该项不能为空", trigger:["change","blur"] },
										]
									},
									{ key:"total_recharge_amount", title:"条件1：累计充值余额", type:"money", width:150, placeholder:"输入金额", incMode: 1, clearable:false, disabled:"lv==0" },
									{ key:"total_order_amount", title:"条件2：累计订单金额", type:"money", width:150, placeholder:"输入金额", incMode: 1, clearable:false, disabled:"lv==0" },
									{
										key: "goods_ids", title: "条件3：购买指定商品任意一个", type: "table-select", placeholder:"选择商品",
										action:"admin/business/sys/goods.getList",
										multiple:true,
										labelFormat:"已选{{length}}个商品",
										formData:{
											status:0
										},
										columns:[
										 { key:"goods_thumb", title:"商品图", type:"avatar", imageWidth:40 },
										 { key:"name", title:"商品名称", type:"text", width:160, align:"left" },
										 { key:"categoryList", title:"所属分类", type:"text", width:140, defaultValue:'暂无',
											formatter: (val, row, column, index) => {
												let str = "";
												val.map((item, index) => {
													if (index > 0) str += "、";
													str += item.name;
												});
												return str;
											}
										 },
										 { key:"sku_list", title:"价格区间", type:"text", width:200, defaultValue:'暂无' ,
											formatter:(val, row, column, index) => {
												let a = [];
												val.map((item, index) => {
													a.push(Number(item.price))
												});
												let max = Math.max(...a);
												let min = Math.min(...a);
												if(max != min){
													// 如果最大值和最小值不相等
													return	vk.pubfn.priceFilter(min) +' - '+ vk.pubfn.priceFilter(max);
												}else{
													// 如果最大值和最小值相等
													return  vk.pubfn.priceFilter(min);
												}
											}
										 },
										 { key: "stock", title: "商品库存", type: "number", width: 110, show:["row"]  },
										],
										queryColumns:[
											{ key: "name", type: "text", title:"商品名称", placeholder:"请输入商品名称", width:220, mode:"%%" },
											{
												key:"category_ids", title:"商品分类", type:"cascader", width:250, mode:"in",
												action:"admin/business/sys/goodsCategory.kh_getList",
												props:{
													list:"rows",
													value:"_id",
													label:"name",
													children:"children",
													checkStrictly:true,
													emitPath:false
													// multiple:true
												}
											},
											{ key: "_id", title: "角色标识", type: "text", width: 150, mode: "%%", show:false }
										],
										showRule:"lv!=0",
									},
									{ key:"is_free_shipping", title:"是否包邮", type:"switch", width:80 },
									{ key:"free_shipping_min_order_amount", title:"包邮最低金额", type:"money", width:150, clearable:false, placeholder:"输入金额",
										disabled:(formData)=>{
											return formData.is_free_shipping ? false : true;
										}
									},
									{ key:"integral_ratio", title:"积分倍率", type:"number", width:150, clearable:false, placeholder:"输入倍率" },
									{ key:"custom_service", title:"会员权益", type:"array<object>", buttonText: "设置", dialog:true, width:120, dialogWidth:700, showRule:"lv!=0",
										size:"small",
										showAdd:true,
										showClear:true,
										showSort:false,
										maxlength:10,
										rowNo:false,
										rightBtns:['delete'],
										defaultValue:{
											name:"",
											comment:"",
										},
										columns:[
											{ key:"name", title:"权益名称", type:"text", minWidth:100, clearable:false,
												isUnique:true,// 不可重复
												rules:[
													{ required:true, message:"该项不能为空", trigger:["change","blur"] },
													{ min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: ["change","blur"] }
												]
											},
											{ key:"comment", title:"描述内容", type:"text", minWidth:150, clearable:false },
										]
									},
								]
							}
						],
						// 表单验证规则
						rules: {

						},
						labelWidth: "100px"
					}
				}
			}
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady(){

		},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {

		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {

		},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options){
				vk.callFunction({
					url: 'client/pub/getMchInfo',
					loading: true,
					data: {

					},
					success: (data) => {
						this.$refs.form1.setFormData(data.mchInfo);
					}
				});
			},
			// 页面跳转
			pageTo(path){
				vk.navigateTo(path);
			},
			// 页面返回
			pageBack(){
				const pages = getCurrentPages();
				if (pages.length>1 && pages[0].route !== that.$page.route) {
					vk.navigateBack();
				} else if(that.page.backPage) {
					vk.navigateTo(that.page.backPage);
				}
			},
			// 表单重置
			resetForm() {
				that.$refs.form1.resetForm();
			},
			// 表单提交
			submitForm(){
				that.$refs.form1.submitForm();
			},
			// 表单提交成功后的回调
			formSuccess(res){
				vk.setVuex("$user.mchInfo",res.data.info);
			}
		},
		// 监听器
		watch:{

		},
		// 计算属性
		computed:{

		}
	}
</script>
<style lang="scss" scoped>

</style>
